﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Factories;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for JobRoleView.xaml
    /// </summary>
    public partial class JobRoleView : UserControl, INavigationAware
    {
        private JobRoleModel Model;
        private readonly IUnityContainer _container;
        private readonly IJobRoleFactory _jobRoleFactory;
        private readonly IJobRoleController _jobRoleController;

        public JobRoleView(IUnityContainer container, IJobRoleFactory jobRoleFactory, IJobRoleController jobRoleController)
        {
            InitializeComponent();

            _container = container;
            _jobRoleController = jobRoleController;
            _jobRoleFactory = jobRoleFactory;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.Count() == 0)
            {
                Model = _jobRoleFactory.CreateNew();
            }
            else
            {
                string id = navigationContext.Parameters["id"].ToString();
                Guid result;

                if (Guid.TryParse(id, out result))
                    Model = _jobRoleFactory.Get(x => x.JobRoleGuid == result);
            }


            this.DataContext = Model;
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            Model = _jobRoleFactory.Upsert(Model);
        }

        private void saveExitButton_Click(object sender, RoutedEventArgs e)
        {
            saveButton_Click(sender, e);
            cancelButton_Click(sender, e);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            _jobRoleController.NavigateToListView();
        }
    }
}
